<template>
  <div>
     <van-button
            v-if="is_followed"
            class="follow-btn"
            round
            size="small"
            :loading="isFollowLoading"
            @click="onFollow"
          >已关注</van-button>

          <van-button
            v-else
            class="follow-btn"
            type="info"
            color="#3296fa"
            round
            :loading="isFollowLoading"
            size="small"
            icon="plus"
            @click="onFollow"
          >关注</van-button>
  </div>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  data () {
    return {
      isFollowLoading: false
    }
  },
  props: {
    is_followed: {
      type: Boolean,
      required: true
    },
    user_id: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async onFollow () {
      this.isFollowLoading = true
      console.log(123)
      try {
        if (this.is_followed) {
          await deleteFollow(this.user_id)
        } else {
          await addFollow(this.user_id)
        }
        this.$emit('update-follow', !this.is_followed)
      } catch (err) {
        console.log(err)
        let message = '操作失败,请重试'
        if (err.response && err.response.status === 400) {
          message = '不能关注自己'
        }
        this.$toast(message)
      }
      this.isFollowLoading = false
    }
  }
}
</script>

<style>

</style>
